import { createRouter, createWebHistory, createWebHashHistory } from 'vue-router'
import { setupLayouts } from 'virtual:generated-layouts'
import generatedRoutes from 'virtual:generated-pages'
import { BASE_LOGIN } from '@/conf'
import { useAccount } from '@pinia/account'

export const routes = setupLayouts(generatedRoutes)
console.log(routes)

const router = createRouter({
  history: createWebHashHistory(),
  routes,
})

router.beforeEach((to: any, from: any, next: Function) => {
  startNprogress()
  const {
    name: toName,
    meta: { label, auth },
  } = to
  const { token } = useAccount()
  if (!token && auth) {
    next({ name: BASE_LOGIN })
    Message.error('请先登录')
  } else {
    next()
  }
})

router.afterEach(() => {
  closeNprogress()
})

export default router

export type IMenuItem = { path?: string; icon?: string; label: string; children?: IMenuItem[] }

export const MENU_LIST: IMenuItem[] = [
  {
    icon: 'ph:airplane-tilt-duotone',
    label: '实用',
    children: [
      { path: '/often/comps', label: '小组件' },
      { path: '/often/stat', label: '拖拽、框选' },
      { path: '/often/rollLoad', label: '滚动加载' },
      { path: '/often/charts', label: 'ECharts' },
      { path: '/often/queue', label: '时间序列' },
      { path: '/often/loaders', label: 'Loading' },
      { path: '/often/responsive', label: '固定大小适配' },
      { path: '/often/postMessage', label: '跨屏交流' },
      { path: '/often/channelMessage', label: '跨屏交流2' },
    ],
  },
  {
    icon: 'ph:airplane-tilt-duotone',
    path: '/table',
    label: '表格',
    children: [
      { path: '/table', label: '表格(:cols风格)' },
      { path: '/table/tradition', label: '传统&虚拟' },
      { path: '/table/transpose', label: '数据转置' },
    ],
  },
  {
    icon: 'ph:airplane-tilt-duotone',
    label: '好Van的',
    children: [
      { path: '/interesting/swiper', label: '轮播 · 翻页' },
      { path: '/interesting/animation', label: '动画' },
      { path: '/interesting/text', label: '打字机' },
      { path: '/interesting/signname', label: '签名' },
      { path: '/interesting/observer', label: 'dom进出视区' },
      { path: '/interesting/touch', label: '↑↓←→' },
      { path: '/interesting/confetti', label: '撒花' },
      { path: '/interesting/driver', label: '教程 / 指引' },
      { path: '/interesting/cursor', label: '鼠标位置反馈' },
      { path: '/interesting/unLoad', label: '阻止窗口关闭' },
      { path: '/interesting/ocr', label: '文字识别' },
    ],
  },
  {
    icon: 'ph:address-book-duotone',
    label: '菜单递归渲染',
    children: [
      {
        label: '第二层',
        children: [
          {
            label: '第三层',
            children: [
              {
                path: '/renderDemo/2',
                label: '第四层',
              },
            ],
          },
        ],
      },
      {
        label: '第二层',
        children: [
          {
            icon: 'fa6-solid:democrat',
            label: '第三层',
            path: '/renderDemo/1',
          },
        ],
      },
    ],
  },
  {
    icon: 'fa6-solid:democrat',
    label: '权限管理',
    children: [
      { path: '/auth/role', label: '角色管理' },
      { path: '/auth/menu', label: '菜单管理' },
      { path: '/auth/user', label: '用户管理' },
    ],
  },
  {
    icon: 'fa6-solid:democrat',
    label: '这个没有用',
    children: [
      { path: '/demo/1', label: 'demo-1' },
      { path: '/demo/2', label: 'demo-2' },
      { path: '/demo/3', label: '一些dialog' },
      { path: '/demo/4', label: '谷歌小恐龙' },
      { path: '/demo/5', label: '滚动监听' },
      { path: '/demo/6', label: 'demo-6' },
      { path: '/demo/7', label: '甘特' },
    ],
  },
]
